package com.itheima.customer.web.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.customer.domain.Customer;
import com.itheima.customer.domain.LinkMan;
import com.itheima.customer.service.ICustomerService;
import com.itheima.customer.service.ILinkManService;
import com.itheima.customer.web.commens.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("linkmanAction")

@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/linkman")

@Results({
@Result(name="addUILinkMan",location="/jsp/linkman/add.jsp"),
@Result(name="addLinkMan",location="/jsp/success.jsp"),
@Result(name="listLinkMan",location="/jsp/linkman/list.jsp"),
@Result(name="removeLinkMan",location="/jsp/success.jsp"),
@Result(name="editUILinkMan",location="/jsp/linkman/edit.jsp"),
@Result(name="editLinkMan",location="/jsp/success.jsp"),

})
public class linkManAction extends ActionSupport implements ModelDriven<LinkMan> {

	@Autowired
	private ILinkManService linkmanService;
	
	@Autowired
	private ICustomerService customerService;
	
	private LinkMan linkman = new LinkMan();
	private List<Customer> customers;
	private Long custId;
	private List<LinkMan> linkmans;
	private String num;

	private PageBean pb;
	

	@Override
	public LinkMan getModel() {
		return linkman;
	}
	
/*	@Action(value="addUILinkMan",results={
			@Result(name="addUILinkMan",type="dispatcher",location="/jsp/linkman/add.jsp")
	})*/
	@Action("addUILinkMan")
	public String addUILinkMan(){
		customers = customerService.findAllCustomers();
		return "addUILinkMan";
	}
	
	@Action("addLinkMan")
	public String addLinkMan(){
		//1.查找联系人所属客户类
		Customer c = customerService.findCustomerById(custId);
		//2.建立联系人和客户的关联关系
		linkman.setCustomer(c);
		//3.保存联系人
		linkmanService.addLinkman(linkman);
		return "addLinkMan";
	}
	
	
	@Action("removeLinkMan")
	public String removeLinkMan(){
		
		linkmanService.removeLinkman(linkman);
		return "removeLinkMan";
	}
	
	
	
	@Action("listLinkMan")
	public String listLinkMan(){
		//1.查找联系人所属客户类
//		Customer c = customerService.findCustomerById(custId);
		//2.建立联系人和客户的关联关系
//		linkman.setCustomer(c);
		//3.保存联系人
		DetachedCriteria dc = DetachedCriteria.forClass(LinkMan.class);
		if(StringUtils.isNotBlank(linkman.getLkmName())){
			dc.add(Restrictions.like("lkmName", "%"+linkman.getLkmName()+"%"));
		}
		if(StringUtils.isNotBlank(linkman.getLkmPosition())){
			dc.add(Restrictions.like("lkmPosition", "%"+linkman.getLkmPosition()+"%"));
		}
		
		if(custId!=null){
			dc.add(Restrictions.eq("customer.custId", custId));
		}
		
		if(StringUtils.isNotBlank(linkman.getLkmGender())){
			dc.add(Restrictions.eq("lkmGender", linkman.getLkmGender()));
		}
		 customers = customerService.findAllCustomers();
		pb = linkmanService.findAllLinkmans(dc ,num);
		return "listLinkMan";
	}
	
	@Action("editUILinkMan")
	public String editUILinkMan(){
		LinkMan dblinkman = (LinkMan) linkmanService.findLinkmanById(linkman.getLkmId());
		ActionContext.getContext().getValueStack().push(dblinkman);
		customers = customerService.findAllCustomers();
		return "editUILinkMan";
	}
	
	
	
	@Action("editLinkMan")
	public String editLinkMan(){
		//1.查找联系人所属客户类
		Customer c = customerService.findCustomerById(custId);
		//2.建立联系人和客户的关联关系
		linkman.setCustomer(c);
		//3.更新联系人
		linkmanService.updateLinkman(linkman);	
		return "editLinkMan";
	}
	
	
	
	public LinkMan getLinkman() {
		return linkman;
	}

	public void setLinkman(LinkMan linkman) {
		this.linkman = linkman;
	}


	public Long getCustId() {
		return custId;
	}

	public void setCustId(Long custId) {
		this.custId = custId;
	}

	public List<LinkMan> getLinkmans() {
		return linkmans;
	}

	public void setLinkmans(List<LinkMan> linkmans) {
		this.linkmans = linkmans;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public PageBean getPb() {
		return pb;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}

	
}






















